var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getPlaneList',(req,res) =>{
    var sql = $sql.plane.count
    var params = req.query ||req.params
    let start = (params.page - 1) * 10;
    var allCount = 0
    console.log(params)
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.plane.get
            conn.query(sql,[start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var planeList = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:planeList
                    }))
                }
            })
        }
    })
})

router.get('/searchPlaneList',(req,res) =>{
    var sql = $sql.plane.searchCount
    var params = req.query ||req.params;
    let start = (params.page - 1) * 10;
    let plane_type = parseInt(params.plane_type);
    var allCount = 0
    console.log(params)
    conn.query(sql,[plane_type], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.plane.searchList
            conn.query(sql,[plane_type,start],function(err,getResult){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var planeList = getResult;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:planeList
                    }))
                }
            })
        }
    })
})

router.post('/addPlane',(req,res)=>{
    var sql = $sql.plane.add
    var params = req.body
    params.plane_buyDate = new Date(params.plane_buyDate);
    params.plane_endDate = new Date(params.plane_endDate);
    params.create_time = new Date();
    params.modify_time = new Date();
    params.admin_id = parseInt(params.admin_id);
    params.plane_id = parseInt(params.plane_id);
    params.plane_type = parseInt(params.plane_type);
    params.touristClassNum = parseInt(params.touristClassNum);
    params.businessClassNum = parseInt(params.businessClassNum);
    params.firstClassNum = parseInt(params.firstClassNum);
    console.log(params)
    conn.query(sql,[params], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/editPlane',(req,res)=>{
    var sql = $sql.plane.update
    var params = req.body
    params.admin_id = parseInt(params.admin_id);
    params.plane_buyDate = new Date(params.plane_buyDate);
    params.plane_endDate = new Date(params.plane_endDate);
    params.modify_time = new Date();
    params.plane_id = parseInt(params.plane_id);
    params.plane_type = parseInt(params.plane_type);
    params.touristClassNum = parseInt(params.touristClassNum);
    params.businessClassNum = parseInt(params.businessClassNum);
    params.firstClassNum = parseInt(params.firstClassNum);
    console.log(params)
    conn.query(sql,[params,params.plane_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/deletePlane',(req,res)=>{
    var sql = $sql.plane.update
    var params = req.body
    params.modify_time = new Date();
    params.admin_id = parseInt(params.admin_id);
    params.is_del = 1;
    console.log(params)
    conn.query(sql, [params,params.plane_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})


module.exports = router;